package com.example.e03;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileReadWriteActivity extends AppCompatActivity {

    private EditText inputText;
    private TextView outputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_read_write);

        inputText = findViewById(R.id.input_text);
        outputText = findViewById(R.id.output_text);

        Button saveButton = findViewById(R.id.save_button);
        Button readButton = findViewById(R.id.read_button);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textToSave = inputText.getText().toString();
                saveToFile(textToSave);
            }
        });

        readButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fileContent = readFromFile();
                outputText.setText(fileContent);
            }
        });
    }

    private void saveToFile(String data) {
        FileOutputStream fos = null;
        BufferedWriter writer = null;
        try {
            // 通过Context类中提供的openFileOutput()方法，返回一个FileOutputStream对象
            fos = openFileOutput("my_file.txt", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(fos));
            // 使用Java流的方式将数据写入到文件中
            writer.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String readFromFile() {
        StringBuilder content = new StringBuilder();
        FileInputStream fis = null;
        BufferedReader reader = null;
        try {
            // 通过Context类中提供的openFileInput()方法，获取一个FileInputStream对象
            fis = openFileInput("my_file.txt");
            // 借助其构建出一个InputStreamReader对象
            InputStreamReader isr = new InputStreamReader(fis);
            // 使用InputStreamReader构建出一个BufferedReader对象
            reader = new BufferedReader(isr);
            String line;
            // 通过BufferedReader进行一行一行读取
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 将读取到的内容返回
        return content.toString();
    }
}